﻿namespace WMS6.BLL
{
    using System;
    using System.Collections.Generic;
    using System.Data;
    using System.Data.Common;
    using WMS6.Common;
    using WMS6.DataEntity;
    using WMS6.DBUtility;
    using WMS6.IDAL;
    using WMS6.QueryEntity;
    using WMS6.DALFactory;

    public class PhysicalRequestBLL
    {
        private static readonly ICommonMethodDA commonMethod = DataAccess.CreateCommonMethod();
        private List<string> dataBaseID;
        private static readonly IInvLotDA invLotDA = DataAccess.CreateInvLotDA();
        private static readonly IInvTransDA invTranDA = DataAccess.CreateInvTransDA();
        private static readonly ILotAttributeDA lotAttributeDA = DataAccess.CreateLotAttributeDA();
        private static readonly IPhysicalRequestDA physicalRequestDA = DataAccess.CreatePhysicalRequestDA();
        private static readonly IPhysicalTagDA physicalTagDA = DataAccess.CreatePhysicalTagDA();
        private PubParasInfo pubParasInfo;
        private static readonly ISysSettingDA sysSettingDA = DataAccess.CreateSysSettingDA();

        public PhysicalRequestBLL()
        {
            this.pubParasInfo = null;
        }

        public PhysicalRequestBLL(string currentWH)
        {
            this.pubParasInfo = null;
            if (!string.IsNullOrEmpty(currentWH))
            {
                this.dataBaseID = commonMethod.GetDataBaseID(currentWH);
                if (this.dataBaseID == null)
                {
                    PublicMethod.GetInstance().ShowAlertMessage("C001");
                }
            }
            else
            {
                PublicMethod.GetInstance().ShowAlertMessage("C002");
            }
        }

        public PhysicalRequestBLL(PubParasInfo tempPubParasInfo)
        {
            this.pubParasInfo = null;
            if (!string.IsNullOrEmpty(tempPubParasInfo.CurrentWH))
            {
                this.dataBaseID = commonMethod.GetDataBaseID(tempPubParasInfo.CurrentWH);
                if (this.dataBaseID == null)
                {
                    PublicMethod.GetInstance().ShowAlertMessage("C001", this.pubParasInfo.UserLanguage);
                }
                else
                {
                    this.pubParasInfo = new PubParasInfo();
                    this.pubParasInfo = tempPubParasInfo;
                }
            }
            else
            {
                PublicMethod.GetInstance().ShowAlertMessage("C002", this.pubParasInfo.UserLanguage);
            }
        }

        public string AdjustInventoryQtyForPc(string PhysicalRequestId)
        {
            string strValue = string.Empty;
            string whLoginID = this.dataBaseID[0];
            DataBase dataBase = new DataBase();
            DbConnection conn = dataBase.connection;
            conn.Open();
            DbTransaction tran = conn.BeginTransaction();
            try
            {
                List<PhysicalTagInfo> PhysicalTagInfos = physicalTagDA.GetPhysicalTagListByID(dataBase, tran, PhysicalRequestId, whLoginID, this.pubParasInfo);
                AdjustmentBLL adjustbll = new AdjustmentBLL();
                if ((PhysicalTagInfos != null) && (PhysicalTagInfos.Count > 0))
                {
                    foreach (PhysicalTagInfo info in PhysicalTagInfos)
                    {
                        if (info.AdjQty != 0M)
                        {
                            adjustbll.AdjustInventoryQty(dataBase, tran, info.Lot, info.Loc, info.LpnID, info.SkuID, info.OwnerID, this.pubParasInfo.UserID, info.AdjQty, whLoginID);
                            this.InsertPCAdjustmentTrans(dataBase, tran, info, whLoginID);
                        }
                    }
                    physicalRequestDA.UpdatePhysicalRequestStatus(dataBase, tran, PhysicalRequestId, PhysicalRequestStatus.Closed, whLoginID);
                    physicalTagDA.UpdatePhysicalTagStatus(dataBase, tran, PhysicalRequestId, PhysicalRequestStatus.Closed, whLoginID);
                    tran.Commit();
                    return strValue;
                }
                return "未找到合适的数据源！";
            }
            catch (Exception ex)
            {
                tran.Rollback();
                CommonErrorHandler.ErrorHandler(ex, LogType.Error);
            }
            finally
            {
                conn.Close();
            }
            return strValue;
        }

        public string DeletePhysicalRequest(List<string> IDS)
        {
            DataBase dataBase = new DataBase();
            DbConnection conn = dataBase.connection;
            conn.Open();
            DbTransaction tran = conn.BeginTransaction();
            string temp = string.Empty;
            try
            {
                List<PhysicalRequestInfo> PhysicalRequestInfos = new List<PhysicalRequestInfo>();
                PhysicalRequestInfo physicalRequestInfo = null;
                foreach (string id in IDS)
                {
                    physicalRequestInfo = physicalRequestDA.GetPhysicalRequestByID(dataBase, tran, id, this.dataBaseID[0], this.pubParasInfo);
                    PhysicalRequestInfos.Add(physicalRequestInfo);
                }
                AuditDataBLL auditDataBLL = new AuditDataBLL();
                physicalTagDA.DeletePhysicalTag(dataBase, tran, IDS, this.dataBaseID[0]);
                if (physicalRequestDA.DeletePhysicalRequest(dataBase, tran, IDS, this.dataBaseID[0]) > 0)
                {
                    for (int i = 0; i < IDS.Count; i++)
                    {
                        auditDataBLL.InsertAuditData<PhysicalRequestInfo>(dataBase, tran, PhysicalRequestInfos[i], null, AuditAction.Delete, this.pubParasInfo.UserID, "PHYSICAL_REQUEST", this.dataBaseID[0]);
                        temp = temp + IDS[i] + ",";
                    }
                    temp = temp.Substring(0, temp.Length - 1);
                }
                tran.Commit();
            }
            catch (Exception ex)
            {
                tran.Rollback();
                CommonErrorHandler.ErrorHandler(ex, LogType.Error);
            }
            finally
            {
                conn.Close();
            }
            return temp;
        }

        public DataSet GetAllPhysicalRequest()
        {
            return physicalRequestDA.GetAllPhysicalRequest(this.dataBaseID[0], this.pubParasInfo);
        }

        public PhysicalRequestInfo GetPCReviewInfoByID(string id)
        {
            string whLoginID = this.dataBaseID[0];
            PhysicalRequestInfo PhysicalRequestEntity = physicalRequestDA.GetPhysicalRequestByID(id, this.dataBaseID[0], this.pubParasInfo);
            if (PhysicalRequestEntity != null)
            {
                PhysicalRequestEntity.PhysicalTagInfos = physicalTagDA.GetPTReviewListByID(id, whLoginID, this.pubParasInfo);
                if (PhysicalRequestEntity.PhysicalTagInfos == null)
                {
                    return PhysicalRequestEntity;
                }
                SysSettingInfo sysSettingInfo = sysSettingDA.GetSysSettingByID("PCSetReCountQtySameAsCountQty", whLoginID, this.pubParasInfo);
                if (sysSettingInfo == null)
                {
                    return PhysicalRequestEntity;
                }
                if (!(sysSettingInfo.Value == "Y"))
                {
                    return PhysicalRequestEntity;
                }
                foreach (PhysicalTagInfo info in PhysicalRequestEntity.PhysicalTagInfos)
                {
                    if ((info.Status == PhysicalTagStatus.RCReleased) || (info.Status == PhysicalTagStatus.RecountPrinted))
                    {
                        info.RecountQty = info.CountQty;
                    }
                }
            }
            return PhysicalRequestEntity;
        }

        public DataSet GetPhysicalRequestDAByQueryEntity(PhysicalRequestQueryEntity RequestEntity)
        {
            List<string> sqlWhere = new List<string>();
            if (!string.IsNullOrEmpty(RequestEntity.PhysicalRequestID))
            {
                sqlWhere.Add(" PR.PHYSICAL_REQUEST_ID " + RequestEntity.PhysicalRequestID);
            }
            if (!string.IsNullOrEmpty(RequestEntity.Status))
            {
                sqlWhere.Add(" PR.STATUS " + RequestEntity.Status);
            }
            if (!string.IsNullOrEmpty(RequestEntity.OwnerIdStart))
            {
                sqlWhere.Add(" PR.OWNER_ID_START " + RequestEntity.OwnerIdStart);
            }
            if (!string.IsNullOrEmpty(RequestEntity.OwnerIdEnd))
            {
                sqlWhere.Add(" PR.OWNER_ID_END " + RequestEntity.OwnerIdEnd);
            }
            if (!string.IsNullOrEmpty(RequestEntity.OwnerIdEnd))
            {
                sqlWhere.Add(" PR.OWNER_ID_END " + RequestEntity.OwnerIdEnd);
            }
            if (!string.IsNullOrEmpty(RequestEntity.SkuIdStart))
            {
                sqlWhere.Add(" PR.SKU_ID_START " + RequestEntity.SkuIdStart);
            }
            if (!string.IsNullOrEmpty(RequestEntity.SkuIdEnd))
            {
                sqlWhere.Add(" PR.SKU_ID_END " + RequestEntity.SkuIdEnd);
            }
            if (!string.IsNullOrEmpty(RequestEntity.LocStart))
            {
                sqlWhere.Add(" PR.LOC_START " + RequestEntity.LocStart);
            }
            if (!string.IsNullOrEmpty(RequestEntity.LocEnd))
            {
                sqlWhere.Add(" PR.LOC_END " + RequestEntity.LocEnd);
            }
            if (!string.IsNullOrEmpty(RequestEntity.LocLevelStart))
            {
                sqlWhere.Add(" PR.LOC_LEVEL_START " + RequestEntity.LocLevelStart);
            }
            if (!string.IsNullOrEmpty(RequestEntity.LocLevelEnd))
            {
                sqlWhere.Add(" PR.LOC_LEVEL_END " + RequestEntity.LocLevelEnd);
            }
            if (!string.IsNullOrEmpty(RequestEntity.ZoneIdStart))
            {
                sqlWhere.Add(" PR.ZONE_ID_START " + RequestEntity.ZoneIdStart);
            }
            if (!string.IsNullOrEmpty(RequestEntity.ZoneIdEnd))
            {
                sqlWhere.Add(" PR.ZONE_ID_END " + RequestEntity.ZoneIdEnd);
            }
            if (!string.IsNullOrEmpty(RequestEntity.AreaIdStart))
            {
                sqlWhere.Add(" PR.AREA_ID_START " + RequestEntity.AreaIdStart);
            }
            if (!string.IsNullOrEmpty(RequestEntity.AreaIdEnd))
            {
                sqlWhere.Add(" PR.AREA_ID_END " + RequestEntity.AreaIdEnd);
            }
            return physicalRequestDA.GetPhysicalRequestByQueryList(sqlWhere, RequestEntity, this.dataBaseID[0], this.pubParasInfo);
        }

        public PhysicalRequestInfo GetPhysicalRequestInfoByID(string id)
        {
            string whLoginID = this.dataBaseID[0];
            PhysicalRequestInfo PhysicalRequestEntity = physicalRequestDA.GetPhysicalRequestByID(id, whLoginID, this.pubParasInfo);
            if (PhysicalRequestEntity != null)
            {
                PhysicalRequestEntity.PhysicalTagInfos = physicalTagDA.GetPhysicalTagListByID(id, whLoginID, this.pubParasInfo);
            }
            return PhysicalRequestEntity;
        }

        public DataSet GetPPCReviewByQueryEntity(PhysicalRequestQueryEntity RequestEntity)
        {
            List<string> sqlWhere = new List<string>();
            if (!string.IsNullOrEmpty(RequestEntity.PhysicalRequestID))
            {
                sqlWhere.Add(" PHYSICAL_REQUEST_ID " + RequestEntity.PhysicalRequestID);
            }
            if (!string.IsNullOrEmpty(RequestEntity.Status))
            {
                sqlWhere.Add(" STATUS " + RequestEntity.Status);
            }
            if (!string.IsNullOrEmpty(RequestEntity.OwnerIdStart))
            {
                sqlWhere.Add(" OWNER_ID_START " + RequestEntity.OwnerIdStart);
            }
            if (!string.IsNullOrEmpty(RequestEntity.OwnerIdEnd))
            {
                sqlWhere.Add(" OWNER_ID_END " + RequestEntity.OwnerIdEnd);
            }
            if (!string.IsNullOrEmpty(RequestEntity.OwnerIdEnd))
            {
                sqlWhere.Add(" OWNER_ID_END " + RequestEntity.OwnerIdEnd);
            }
            if (!string.IsNullOrEmpty(RequestEntity.SkuIdStart))
            {
                sqlWhere.Add(" SKU_ID_START " + RequestEntity.SkuIdStart);
            }
            if (!string.IsNullOrEmpty(RequestEntity.SkuIdEnd))
            {
                sqlWhere.Add(" SKU_ID_END " + RequestEntity.SkuIdEnd);
            }
            if (!string.IsNullOrEmpty(RequestEntity.LocStart))
            {
                sqlWhere.Add(" LOC_START " + RequestEntity.LocStart);
            }
            if (!string.IsNullOrEmpty(RequestEntity.LocEnd))
            {
                sqlWhere.Add(" LOC_END " + RequestEntity.LocEnd);
            }
            if (!string.IsNullOrEmpty(RequestEntity.LocLevelStart))
            {
                sqlWhere.Add(" LOC_LEVEL_START " + RequestEntity.LocLevelStart);
            }
            if (!string.IsNullOrEmpty(RequestEntity.LocLevelEnd))
            {
                sqlWhere.Add(" LOC_LEVEL_END " + RequestEntity.LocLevelEnd);
            }
            if (!string.IsNullOrEmpty(RequestEntity.ZoneIdStart))
            {
                sqlWhere.Add(" ZONE_ID_START " + RequestEntity.ZoneIdStart);
            }
            if (!string.IsNullOrEmpty(RequestEntity.ZoneIdEnd))
            {
                sqlWhere.Add(" ZONE_ID_END " + RequestEntity.ZoneIdEnd);
            }
            if (!string.IsNullOrEmpty(RequestEntity.AreaIdStart))
            {
                sqlWhere.Add(" AREA_ID_START " + RequestEntity.AreaIdStart);
            }
            if (!string.IsNullOrEmpty(RequestEntity.AreaIdEnd))
            {
                sqlWhere.Add(" AREA_ID_END " + RequestEntity.AreaIdEnd);
            }
            sqlWhere.Add(" STATUS " + RequestEntity.AreaIdEnd);
            return physicalRequestDA.GetPhysicalRequestByQueryList(sqlWhere, RequestEntity, this.dataBaseID[0], this.pubParasInfo);
        }

        private void InsertPCAdjustmentTrans(DataBase dataBase, DbTransaction tran, PhysicalTagInfo physicaltag, string whLoginID)
        {
            InvTransInfo invTransInfo = new InvTransInfo {
                TransID = commonMethod.GenNextNumStr(dataBase, tran, "INV_TRANS", whLoginID),
                CaseCnt = 0M,
                Cube = 0.0,
                Descr = "",
                GrossWgt = 0.0,
                InnerPack = 0M,
                Lot = physicaltag.Lot,
                NetWgt = 0.0,
                OtherUnit1 = 0.0,
                OtherUnit2 = 0.0,
                OwnerID = physicaltag.OwnerID,
                PackID = physicaltag.PackID,
                Pallet = 0M,
                Qty = physicaltag.AdjQty,
                ReceiptID = "",
                ReceiptLineID = 0,
                DocID = physicaltag.PhysicalRequestID,
                DocLineID = physicaltag.TagNo,
                SkuID = physicaltag.SkuID,
                Status = TransStatus.OK,
                TransType = TransType.Adjustment,
                UomID = physicaltag.UomID,
                SourceTransType = TransSourceType.CycleCountAdd,
                ReceiptDate = DateTime.Now,
                FromLoc = physicaltag.Loc,
                FromLpnID = physicaltag.LpnID,
                ToLoc = physicaltag.Loc,
                ToLpnID = physicaltag.LpnID
            };
            LotAttributeInfo lotAttributeInfo = lotAttributeDA.GetLotAttributeByID(dataBase, tran, physicaltag.Lot, whLoginID);
            invTransInfo.LotAttr01 = lotAttributeInfo.LotAttr01;
            invTransInfo.LotAttr02 = lotAttributeInfo.LotAttr02;
            invTransInfo.LotAttr03 = lotAttributeInfo.LotAttr03;
            invTransInfo.LotAttr04 = lotAttributeInfo.LotAttr04;
            invTransInfo.LotAttr05 = lotAttributeInfo.LotAttr05;
            invTransInfo.LotAttr06 = lotAttributeInfo.LotAttr06;
            invTransInfo.LotAttr07 = lotAttributeInfo.LotAttr07;
            invTransInfo.LotAttr08 = lotAttributeInfo.LotAttr08;
            invTransInfo.LotAttr09 = lotAttributeInfo.LotAttr09;
            invTransInfo.LotAttr10 = lotAttributeInfo.LotAttr10;
            invTransInfo.CreatedBy = physicaltag.CreatedBy;
            invTransInfo.UpdatedBy = invTransInfo.CreatedBy;
            invTransInfo.TransDate = physicaltag.CountDate;
            InvLotInfo invLotInfo = invLotDA.GetInvLotByID(dataBase, tran, physicaltag.Lot, whLoginID);
            if (invLotInfo != null)
            {
                invTransInfo.UnitPrice = invLotInfo.UnitPrice;
            }
            invTranDA.InsertInvTrans(dataBase, tran, invTransInfo, whLoginID);
        }

        public string InsertPhysicalRequest(PhysicalRequestInfo obj)
        {
            string whLoginID = this.dataBaseID[0];
            DataBase dataBase = new DataBase();
            DbConnection conn = dataBase.connection;
            conn.Open();
            DbTransaction tran = conn.BeginTransaction();
            string PhysicalRequest = string.Empty;
            try
            {
                if (string.IsNullOrEmpty(obj.PhysicalRequestID))
                {
                    obj.PhysicalRequestID = commonMethod.GenNextNumStr(dataBase, tran, "PHYSICAL_REQUEST", whLoginID);
                }
                obj.UpdatedBy = obj.CreatedBy;
                obj.CreatedDate = DateTime.Now;
                obj.UpdatedDate = DateTime.Now;
                if (physicalRequestDA.InsertPhysicalRequest(dataBase, tran, obj, whLoginID) > 0)
                {
                    PhysicalRequest = obj.PhysicalRequestID;
                    this.SelectInventoryByPhysicalRequest(dataBase, tran, obj, whLoginID);
                    new AuditDataBLL().InsertAuditData<PhysicalRequestInfo>(dataBase, tran, null, obj, AuditAction.Insert, this.pubParasInfo.UserID, "PHYSICAL_REQUEST", this.dataBaseID[0]);
                }
                tran.Commit();
            }
            catch (Exception ex)
            {
                tran.Rollback();
                CommonErrorHandler.ErrorHandler(ex, LogType.Error);
            }
            finally
            {
                conn.Close();
            }
            return PhysicalRequest;
        }

        public string InvHoldForPC(string id, string strType)
        {
            string strValue = string.Empty;
            string whLoginID = this.dataBaseID[0];
            DataBase dataBase = new DataBase();
            DbConnection conn = dataBase.connection;
            conn.Open();
            DbTransaction tran = conn.BeginTransaction();
            try
            {
                List<PhysicalTagInfo> PhysicalTagInfos = physicalTagDA.GetPhysicalTagListByID(dataBase, tran, id, whLoginID, this.pubParasInfo);
                if ((PhysicalTagInfos != null) && (PhysicalTagInfos.Count > 0))
                {
                    InvHoldBLL InvHoldBll = new InvHoldBLL(this.pubParasInfo);
                    foreach (PhysicalTagInfo info in PhysicalTagInfos)
                    {
                        InvHoldInfo invHoldInfo = new InvHoldInfo {
                            Lot = info.Lot,
                            OwnerID = info.OwnerID,
                            HoldBy = this.pubParasInfo.UserID,
                            Loc = info.Loc,
                            SkuID = info.SkuID,
                            LpnID = info.LpnID,
                            HoldType = "LotLocLPN",
                            UpdatedBy = this.pubParasInfo.UserID
                        };
                        string CS40003 = strType.ToUpper();
                        if (CS40003 != null)
                        {
                            if (!(CS40003 == "OK"))
                            {
                                if (CS40003 == "HOLD")
                                {
                                    goto Label_014A;
                                }
                            }
                            else
                            {
                                invHoldInfo.IsHold = "Y";
                            }
                        }
                        goto Label_0159;
                    Label_014A:
                        invHoldInfo.IsHold = "N";
                    Label_0159:
                        InvHoldBll.InvHoldByLotLocLpn(dataBase, tran, invHoldInfo, strType);
                    }
                    tran.Commit();
                    return strValue;
                }
                return "未找到合适的数据源！";
            }
            catch (Exception ex)
            {
                tran.Rollback();
                CommonErrorHandler.ErrorHandler(ex, LogType.Error);
            }
            finally
            {
                conn.Close();
            }
            return strValue;
        }

        public void SelectInventoryByPhysicalRequest(DataBase database, DbTransaction tran, PhysicalRequestInfo prhsicalRequestInfo, string whLoginID)
        {
            if (prhsicalRequestInfo != null)
            {
                physicalTagDA.DeletePhysicalTag(database, tran, new List<string> { prhsicalRequestInfo.PhysicalRequestID }, whLoginID);
                List<string> Sqlwhere = new List<string>();
                if (!string.IsNullOrEmpty(prhsicalRequestInfo.OwnerIdStart))
                {
                    Sqlwhere.Add(" INV.OWNER_ID >= '" + prhsicalRequestInfo.OwnerIdStart + "'");
                }
                if (!string.IsNullOrEmpty(prhsicalRequestInfo.OwnerIdEnd))
                {
                    Sqlwhere.Add(" INV.OWNER_ID <= '" + prhsicalRequestInfo.OwnerIdEnd + "'");
                }
                if (!string.IsNullOrEmpty(prhsicalRequestInfo.SkuIdStart))
                {
                    Sqlwhere.Add(" INV.SKU_ID >= '" + prhsicalRequestInfo.SkuIdStart + "'");
                }
                if (!string.IsNullOrEmpty(prhsicalRequestInfo.SkuIdEnd))
                {
                    Sqlwhere.Add(" INV.SKU_ID <= '" + prhsicalRequestInfo.SkuIdEnd + "'");
                }
                if (!string.IsNullOrEmpty(prhsicalRequestInfo.LocStart))
                {
                    Sqlwhere.Add(" INV.LOC >= '" + prhsicalRequestInfo.LocStart + "'");
                }
                if (!string.IsNullOrEmpty(prhsicalRequestInfo.LocEnd))
                {
                    Sqlwhere.Add(" INV.LOC <= '" + prhsicalRequestInfo.LocEnd + "'");
                }
                if (prhsicalRequestInfo.LocLevelStart != 0)
                {
                    Sqlwhere.Add(" L.LOC_LEVEL >= " + prhsicalRequestInfo.LocLevelStart);
                }
                if (prhsicalRequestInfo.LocLevelEnd != 0)
                {
                    Sqlwhere.Add(" L.LOC_LEVEL <= " + prhsicalRequestInfo.LocLevelEnd);
                }
                if (!string.IsNullOrEmpty(prhsicalRequestInfo.ZoneIdStart))
                {
                    Sqlwhere.Add(" L.ZONE_ID >= '" + prhsicalRequestInfo.ZoneIdStart + "'");
                }
                if (!string.IsNullOrEmpty(prhsicalRequestInfo.ZoneIdEnd))
                {
                    Sqlwhere.Add(" L.ZONE_ID <= '" + prhsicalRequestInfo.ZoneIdEnd + "'");
                }
                if (!string.IsNullOrEmpty(prhsicalRequestInfo.AreaIdStart))
                {
                    Sqlwhere.Add(" A.AREA_ID >= '" + prhsicalRequestInfo.AreaIdStart + "'");
                }
                if (!string.IsNullOrEmpty(prhsicalRequestInfo.AreaIdEnd))
                {
                    Sqlwhere.Add(" A.AREA_ID <= '" + prhsicalRequestInfo.AreaIdEnd + "'");
                }
                if (prhsicalRequestInfo.OnlyNonEmptyLoc == "Y")
                {
                    Sqlwhere.Add("INV.QTY>0");
                }
                List<PhysicalCountInventory> InventoryInfo = physicalRequestDA.SelectInventoryByPhysicalRequest(database, tran, Sqlwhere, this.pubParasInfo.UserID, whLoginID);
                if (InventoryInfo.Count > 0)
                {
                    try
                    {
                        for (int i = 0; InventoryInfo.Count > i; i++)
                        {
                            PhysicalTagInfo physicalTagInfo;
                            physicalTagInfo = new PhysicalTagInfo();
                            physicalTagInfo.PhysicalRequestID = prhsicalRequestInfo.PhysicalRequestID;
                            physicalTagInfo.TagNo = i + 1;
                            physicalTagInfo.GroupNo = 0;
                            physicalTagInfo.TeamNo = "A";
                            physicalTagInfo.OwnerID = InventoryInfo[i].OwnerID;
                            physicalTagInfo.SkuID = InventoryInfo[i].SkuID;
                            physicalTagInfo.Lot = InventoryInfo[i].Lot;
                            physicalTagInfo.Loc = InventoryInfo[i].Loc;
                            physicalTagInfo.LpnID = InventoryInfo[i].LpnID;
                            physicalTagInfo.SysQty = InventoryInfo[i].Qty;
                            physicalTagInfo.Status = prhsicalRequestInfo.Status;
                            physicalTagInfo.StatusMsg = "";
                            physicalTagInfo.CreatedBy = prhsicalRequestInfo.CreatedBy;
                            physicalTagInfo.CreatedDate = prhsicalRequestInfo.CreatedDate;
                            physicalTagInfo.UpdatedBy = physicalTagInfo.CreatedBy;
                            physicalTagInfo.UpdatedDate = physicalTagInfo.UpdatedDate;
                            physicalTagInfo.PackID = InventoryInfo[i].PakcID;
                            physicalTagInfo.UomID = InventoryInfo[i].UomID;
                            physicalTagDA.InsertPhysicalTag(database, tran, physicalTagInfo, whLoginID);
                        }
                    }
                    catch (Exception ex)
                    {
                        CommonErrorHandler.ErrorHandler(ex, LogType.Error);
                    }
                }
            }
        }

        public string UpdatePhysicalRequest(PhysicalRequestInfo obj)
        {
            string whLoginID = this.dataBaseID[0];
            DataBase dataBase = new DataBase();
            DbConnection conn = dataBase.connection;
            conn.Open();
            DbTransaction tran = conn.BeginTransaction();
            string PhysicalRequest = string.Empty;
            try
            {
                PhysicalRequestInfo oldPhysicalRequestInfo = physicalRequestDA.GetPhysicalRequestByID(dataBase, tran, obj.PhysicalRequestID, this.dataBaseID[0], this.pubParasInfo);
                if (physicalRequestDA.UpdatePhysicalRequest(dataBase, tran, obj, whLoginID) > 0)
                {
                    PhysicalRequest = obj.PhysicalRequestID;
                    this.SelectInventoryByPhysicalRequest(dataBase, tran, obj, whLoginID);
                    new AuditDataBLL().InsertAuditData<PhysicalRequestInfo>(oldPhysicalRequestInfo, obj, AuditAction.Update, this.pubParasInfo.UserID, "PHYSICAL_REQUEST", this.dataBaseID[0]);
                }
                tran.Commit();
            }
            catch (Exception ex)
            {
                tran.Rollback();
                CommonErrorHandler.ErrorHandler(ex, LogType.Error);
            }
            finally
            {
                conn.Close();
            }
            return PhysicalRequest;
        }
    }
}

